package utilidades.number;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.InvalidationListener;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.SimpleFloatProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.Control;
import javafx.scene.control.TextField;
import javafx.util.converter.NumberStringConverter;

/**
 * Fertonani oscar
 */
public class PorcentajeTextField extends TextField {

    private SimpleFloatProperty number = new SimpleFloatProperty();
    //Si quiero q no tenga puntos ni nada....
    // NumberFormat nf =  new DecimalFormat("###");
    //Si quiero q este separado por tres puntos
    DecimalFormat nf = new DecimalFormat("###.##%");

    public final Float getNumber() {
        return number.get();
    }

    private final void setNumber(Float value) {
        number.set(value);
    }

    public SimpleFloatProperty numberProperty() {
        return number;
    }

    public PorcentajeTextField() {


        super();

        /*
         // simbolo.setDecimalSeparator('-');
         simbolo.setGroupingSeparator('-');
         final  DecimalFormat nf =  new DecimalFormat("###,###",simbolo);
         */

        

        this.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> textProperty, String oldValue, String texto) {
                if (texto != null && !texto.equals(oldValue) ) {
                    if (getText().isEmpty()) {
                        number.set(Float.valueOf(0));
                        return;
                    }
                    if (texto.matches("\\d+,?(\\d+)?(%)?")) {
                        try {
                            String ultimoI = getText().substring(getText().length());
                            Number n;
                            if (!ultimoI.equals("%")) {
                                
                                n = nf.parse(getText() + "%");
                                number.set(n.floatValue());
                               
                                return;
                            }
                            if (!ultimoI.equals(",")) {
                               return;
                            }

                            n = nf.parse(getText());
                            number.set(n.floatValue());
                           

                        } catch (ParseException ex) {
                            Logger.getLogger(PorcentajeTextField.class.getName()).log(Level.SEVERE, null, ex);
                        }

                    }else{
                        setText(oldValue);
                    }
                }
            }
        });


        focusedProperty()
                .addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (!newValue.booleanValue()) {
                  setText(nf.format(number.get()));
            }
            }
        });
    }



    public void setValue(Float value) {
        
        setText(nf.format(value));
    }
}
